package com.testnetty.controller;
import com.testnetty.commot.NettyServerHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testNettyController")
public class TestNettyController {

    /**
     * 服务器查看当前所有连接的客户端
     * @return
     */
    @GetMapping("/getClients")
    public String getClients(){
        StringBuilder stringBuilder = new StringBuilder();
        NettyServerHandler.channelGroup.forEach(item->{
            stringBuilder.append(item.remoteAddress()).append(";");
        });
        return stringBuilder.toString();
    }

    /**
     * 服务器获取到所有客户单的ip地址及端口号后，即可通过其给指定客户端发送数据
     * @return
     */
    @GetMapping("/sendMsgToClient")
    public String sendMsgToClient(@RequestParam String remoteIpAndPort, @RequestParam String content){
        StringBuilder stringBuilder = new StringBuilder();
        NettyServerHandler.channelGroup.forEach(item->{
            if(remoteIpAndPort.equals(item.remoteAddress().toString())){
                item.writeAndFlush(content);
                stringBuilder.append("相等发送");
            }else{
                stringBuilder.append("不相等不发送");
            }
        });
        return stringBuilder.toString();
    }



}
